﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML dir=ltr XMLNS:MSHelp = "http://msdn.microsoft.com/mshelp" xmlns:ddue = 
"http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink = 
"http://www.w3.org/1999/xlink" xmlns:tool = "http://www.microsoft.com/tooltip" 
XMLNS:[default] http://ddue.schemas.microsoft.com/authoring/2003/5 = 
"http://ddue.schemas.microsoft.com/authoring/2003/5"><HEAD><TITLE>Session Sample</TITLE>
<META content="text/html; CHARSET=utf-8" http-equiv=Content-Type></META>
<META name=save content=history></META><LINK rel=stylesheet type=text/css 
href="../../../../../CommonFiles/Classic.css"></LINK>

<META name=GENERATOR content="MSHTML 8.00.6001.18702"></HEAD>
<BODY><INPUT id=userDataCache class=userDataStyle type=hidden></INPUT><INPUT 
id=hiddenScrollOffset type=hidden></INPUT><IMG 
style="WIDTH: 0px; DISPLAY: none; HEIGHT: 0px" id=dropDownImage 
src="../../../../../../Common/Html/drpdown.gif"></IMG><IMG 
style="WIDTH: 0px; DISPLAY: none; HEIGHT: 0px" id=dropDownHoverImage 
src="../../../../../../Common/Html/drpdown_orange.gif"></IMG><IMG 
style="WIDTH: 0px; DISPLAY: none; HEIGHT: 0px" id=copyImage 
src="../../../../../../Common/Html/copycode.gif"></IMG><IMG 
style="WIDTH: 0px; DISPLAY: none; HEIGHT: 0px" id=copyHoverImage 
src="../../../../../../Common/Html/copycodeHighlight.gif"></IMG>
<DIV id=header>
<TABLE id=topTable width="100%">
  <TBODY>
  <TR id=headerTableRow1>
    <TD align=left><SPAN id=runningHeaderText></SPAN></TD></TR>
  <TR id=headerTableRow2>
    <TD align=left><SPAN id=nsrTitle>Session Sample</SPAN></TD></TR>
  <TR id=headerTableRow3>
    <TD></TD></TR></TBODY></TABLE>
</DIV>
<DIV id=mainSection>
<DIV id=mainBody>
<DIV id=allHistory class=saveHistory onload="loadAll()" 
onsave="saveAll()"></DIV>
<P>This sample demonstrates using the <B>WS2007HttpRelayBinding</B> binding with 
reliable session enabled. It also shows how to specify Service Bus credentials in 
    configuration instead of doing so programmatically.</P>
<H2 class=heading>Prerequisites</H2>
<DIV id=sectionSection0 class=section>
<content xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
    <p xmlns="">
        If you haven&#39;t already done so, read the release notes document that explains 
        how to sign up for a Windows Azure account and how to 
        configure your environment.</p>
    </content>
    </DIV>
    <H2 class=heading>Service</H2>
<DIV id=sectionSection1 class=section><content 
xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
<P xmlns="">The service project defines a simple session contract 
(<CODE>IPingContract</CODE>):</P>
<DIV class=code xmlns=""><SPAN codeLanguage="CSharp">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>C#&nbsp;</TH>
    <TH><SPAN class=copyCode onfocusin=ChangeCopyCodeIcon(this) 
      onmouseover=ChangeCopyCodeIcon(this) onfocusout=ChangeCopyCodeIcon(this) 
      tabIndex=0 onkeypress=CopyCode_CheckKey(this) 
      onmouseout=ChangeCopyCodeIcon(this) onclick=CopyCode(this)></SPAN></TH></TR>
  <TR>
    <TD colSpan=2><PRE>[ServiceContract(SessionMode = SessionMode.Required, Name = "IPingContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
public interface IPingContract
{
    [OperationContract(IsInitiating = true, IsTerminating = false)]
    void Open();
 
    [OperationContract(IsInitiating = false, IsOneWay = true, IsTerminating = false)]
    void Ping(int count);
 
    [OperationContract(IsInitiating = false, IsTerminating = true)]
    void Close();
}</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns="">The <CODE>Open</CODE> operation is used to initiate a session. 
<CODE>Ping</CODE> is a one-way operation that can be called an arbitrary number 
of times. The <CODE>Close</CODE> operation terminates the sequence. The service 
implements this contract in the <CODE>PingService</CODE> class. </P>
<P xmlns="">The endpoints for this service are defined in the application 
configuration file, as follows:</P>
<DIV class=code xmlns=""><SPAN codeLanguage="xml">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>Xml&nbsp;</TH>
    <TH><SPAN class=copyCode onfocusin=ChangeCopyCodeIcon(this) 
      onmouseover=ChangeCopyCodeIcon(this) onfocusout=ChangeCopyCodeIcon(this) 
      tabIndex=0 onkeypress=CopyCode_CheckKey(this) 
      onmouseout=ChangeCopyCodeIcon(this) onclick=CopyCode(this)></SPAN></TH></TR>
  <TR>
    <TD colSpan=2><PRE>&lt;service name="Microsoft.ServiceBus.Samples.PingService"&gt;
    &lt;endpoint name="ServiceBusEndpoint"
                       contract="Microsoft.ServiceBus.Samples.IPingContract"
                       bindingConfiguration="default"
                       binding="ws2007HttpRelayBinding"
                       behaviorConfiguration="sharedSecretClientCredentials" /&gt;
&lt;/service&gt;</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns=""><br />This endpoint is configured to use a binding of type 
<B>WS2007HttpRelayBinding</B>. It references (via the <B>bindingConfiguration</B> 
attribute) a binding configuration named <CODE>default</CODE>:</P>
<DIV class=code xmlns=""><SPAN codeLanguage="xml">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>Xml&nbsp;</TH>
    <TH><SPAN class=copyCode onfocusin=ChangeCopyCodeIcon(this) 
      onmouseover=ChangeCopyCodeIcon(this) onfocusout=ChangeCopyCodeIcon(this) 
      tabIndex=0 onkeypress=CopyCode_CheckKey(this) 
      onmouseout=ChangeCopyCodeIcon(this) onclick=CopyCode(this)></SPAN></TH></TR>
  <TR>
    <TD colSpan=2><PRE> &lt;bindings&gt;
    &lt;!-- Application Binding --&gt;
    &lt;ws2007HttpRelayBinding&gt;
        &lt;binding name="default"&gt;
            &lt;reliableSession enabled="true" /&gt;
            &lt;security mode="Transport" relayClientAuthenticationType="RelayAccessToken"/&gt;
        &lt;/binding&gt;
    &lt;/ws2007HttpRelayBinding&gt;
&lt;/bindings&gt;</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns=""><br />The endopint also references a behavior configuration (via the 
    <b>behaviorConfiguration</b> attribute) named <CODE>sharedSecretClientCredentials</CODE>.
    <br/>This is where you can specify the issuer name and secret to be used by the service to authenticate with the Service Bus:</P>
<DIV class=code xmlns=""><SPAN codeLanguage="xml">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>Xml&nbsp;</TH>
    <TH><SPAN class=copyCode onfocusin=ChangeCopyCodeIcon(this) 
      onmouseover=ChangeCopyCodeIcon(this) onfocusout=ChangeCopyCodeIcon(this) 
      tabIndex=0 onkeypress=CopyCode_CheckKey(this) 
      onmouseout=ChangeCopyCodeIcon(this) onclick=CopyCode(this)></SPAN></TH></TR>
  <TR>
    <TD colSpan=2><PRE>&lt;behaviors&gt;      
    &lt;endpointBehaviors&gt;
        &lt;behavior name="sharedSecretClientCredentials"&gt;
            &lt;transportClientEndpointBehavior credentialType="SharedSecret"&gt;
                &lt;clientCredentials&gt;
                    &lt;sharedSecret issuerName="<b>ISSUER_NAME</b>" issuerSecret="<b>ISSUER_SECRET</b>" /&gt;
                &lt;/clientCredentials&gt;
            &lt;/transportClientEndpointBehavior&gt;
        &lt;/behavior&gt;
    &lt;/endpointBehaviors&gt;
&lt;/behaviors&gt;</PRE></TD></TR></TBODY></TABLE></SPAN></DIV></content></DIV>
<H2 class=heading>Client</H2>
<DIV id=sectionSection2 class=section><content 
xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
<P xmlns="">The client is configured (again, via the application configuration 
file) with the following endpoint:</P>
<DIV class=code xmlns=""><SPAN codeLanguage="xml">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>Xml&nbsp;</TH>
    <TH><SPAN class=copyCode onfocusin=ChangeCopyCodeIcon(this) 
      onmouseover=ChangeCopyCodeIcon(this) onfocusout=ChangeCopyCodeIcon(this) 
      tabIndex=0 onkeypress=CopyCode_CheckKey(this) 
      onmouseout=ChangeCopyCodeIcon(this) onclick=CopyCode(this)></SPAN></TH></TR>
  <TR>
    <TD colSpan=2><PRE> &lt;client&gt;
    &lt;!-- Application Endpoint --&gt;
    &lt;endpoint name="ServiceBusEndpoint"
                       binding="ws2007HttpRelayBinding"
                       contract="Microsoft.ServiceBus.Samples.IPingContract"
                       behaviorConfiguration="sharedSecretClientCredentials"
                       bindingConfiguration="default" /&gt;
&lt;/client&gt;</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns="">In the code, an endpoint is opened.</P>
<DIV class=code xmlns=""><SPAN codeLanguage="CSharp">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>C#&nbsp;</TH>
    <TH><SPAN class=copyCode onfocusin=ChangeCopyCodeIcon(this) 
      onmouseover=ChangeCopyCodeIcon(this) onfocusout=ChangeCopyCodeIcon(this) 
      tabIndex=0 onkeypress=CopyCode_CheckKey(this) 
      onmouseout=ChangeCopyCodeIcon(this) onclick=CopyCode(this)></SPAN></TH></TR>
  <TR>
    <TD colSpan=2><PRE>Console.Write("Your Service Namespace: ");
string serviceNamespace = Console.ReadLine();

Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("https", serviceNamespace, "PingService");
ChannelFactory<IPingContract> channelFactory = new ChannelFactory<IPingContract>("ServiceBusEndpoint", new EndpointAddress(serviceUri));

IPingContract channel = channelFactory.CreateChannel();
Console.WriteLine("Opening Channel.");
channel.Open();</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns="">After the ChannelFactory has been created, the client application 
creates a channel to the service and then interacts with it. Once the 
interaction is complete, the client closes the channel and the 
<CODE>ChannelFactory</CODE>, then exits.</P></content></DIV>
<H2 class=heading>Building and Running the Sample</H2>
<DIV id=sectionSection3 class=section><content 
xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
<P xmlns="">Follow the steps below to run the application: </P>
<OL class=ordered xmlns="">
  <li>Open the App.config files in both the Service and Client projects and replace the strings <b>ISSUER_NAME</b> and <b>ISSUER_SECRET</b> with the issuer name and secret you want to use.
  <br />Note that you may use the same values in both projects or alternately, you can set up multiple issuers and use different values for the Service and Client.<br /><br /></li>  
  <li>Build the Service and Client projects.<br /><br /></li>
  <LI>From a command prompt with elevated privileges, run the service 
  (Service\bin\Debug\Service.exe).<BR><BR>
  <LI>When prompted, provide the service namespace you want to use.<br />
      At this point, the 
  service should indicate that it is listening at the configured 
address.<BR><BR>
  <LI>From another command prompt, run the client 
  (Client\bin\Debug\Client.exe).<BR><BR>
  <LI>Provide the service namespace you want to connect to.<BR><BR>
  </OL>
<P xmlns="">When finished, press <B>Enter</B> to exit the client and the 
service.</P>
<P xmlns=""><B>Expected Output – Client</B></P>
<DIV class=code xmlns=""><SPAN codeLanguage="other">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE>Your Service Namespace: &lt;service namespace&gt;
Opening Channel.
Ping: 1
Ping: 2
Ping: 3
Ping: 4
Ping: 5
Ping: 6
Ping: 7
Ping: 8
Ping: 9
Ping: 10
Ping: 11
Ping: 12
Ping: 13
Ping: 14
Ping: 15
Ping: 16
Ping: 17
Ping: 18
Ping: 19
Ping: 20
Ping: 21
Ping: 22
Ping: 23
Ping: 24
Ping: 25
Closing Channel.</PRE></TD></TR></TBODY></TABLE></SPAN></DIV>
<P xmlns=""><B>Expected Output – Service</B></P>
<DIV class=code xmlns=""><SPAN codeLanguage="other">
<TABLE cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TH>&nbsp;</TH>
</TR>
  <TR>
    <TD colSpan=2><PRE><SPAN codeLanguage="other">Your Service Namespace: &lt;service namespace&gt;
Service address: https://&lt;serviceNamespace&gt;.servicebus.windows.net/PingService/</SPAN>
Press [Enter] to exit
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Opened.
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 1
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 2
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 3
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 4
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 5
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 6
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 7
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 8
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 9
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 10
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 11
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 12
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 13
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 14
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 15
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 16
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 17
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 18
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 19
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 20
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 21
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 22
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 23
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 24
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Ping: 25
Session (urn:uuid:df3910e7-28f4-4e19-a6f1-4dbcd13289e2) Closed.</PRE></TD></TR></TBODY></TABLE></SPAN></DIV></content></DIV><!--[if gte IE 5]><tool:tip 
avoidmouse="false" element="languageFilterToolTip"></tool:tip><![endif]--></DIV>
<P xmlns="">
    <hr /> 
    Did you find this information useful?
    <a href="http://go.microsoft.com/fwlink/?LinkID=155664">
        Please send your suggestions and comments about the documentation.

    </a></P></DIV></BODY></HTML>
